package com.crane.framework.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crane.framework.entity.Org;
import com.crane.framework.entity.UserOrg;
import com.crane.framework.exception.RRException;
import com.crane.framework.mapper.OrgMapper;
import com.crane.framework.service.IOrgService;
import com.crane.framework.service.IUserOrgService;
import com.crane.framework.mapper.UserOrgMapper;
import com.crane.framework.utils.CommonUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 47349
 * @description 针对表【sys_user_org(用户组织机构表)】的数据库操作Service实现
 * @createDate 2024-02-04 01:28:26
 */
@Service
public class UserOrgServiceImpl extends ServiceImpl<UserOrgMapper, UserOrg>
    implements IUserOrgService {
  @Resource private OrgMapper orgMapper;

  @Override
  public List<String> getUserIdListByOrgIds(List<String> orgIds) {
    List<UserOrg> userOrgList = this.lambdaQuery().in(UserOrg::getOrgId, orgIds).list();
    if (CommonUtil.isNullOrEmpty(userOrgList)) {
      return new ArrayList<>();
    }
    return userOrgList.stream().map(UserOrg::getUserId).collect(Collectors.toList());
  }

  @Override
  public void setUser2Org(String userId, String orgId) {
    Long count =
        this.lambdaQuery().eq(UserOrg::getUserId, userId).eq(UserOrg::getOrgId, orgId).count();
    if (count > 0) {
      throw new RRException("该用户已经属于该组织");
    }

    UserOrg userOrg = new UserOrg();
    userOrg.setUserId(userId);
    userOrg.setOrgId(orgId);
    this.save(userOrg);
  }

  @Override
  public Map<String, List<String>> getUserId2OrgNameMap(List<String> userId) {
    List<UserOrg> userOrgList = this.lambdaQuery().in(UserOrg::getUserId, userId).list();
    if (CommonUtil.isNullOrEmpty(userOrgList)) {
      return null;
    }

    List<String> orgIdList = userOrgList.stream().map(UserOrg::getOrgId).toList();
    List<Org> orgList = orgMapper.selectBatchIds(orgIdList);

    if (CommonUtil.isNullOrEmpty(orgList)) {
      return null;
    }

    Map<String, String> orgId2OrgNameMap =
        orgList.stream().collect(Collectors.toMap(Org::getId, Org::getName));

    Map<String, List<String>> map = new HashMap<>();

    userOrgList.forEach(
        i -> {
          if (!map.containsKey(i.getUserId())) {
            List<String> orgNameList = new ArrayList<>();
            orgNameList.add(orgId2OrgNameMap.get(i.getOrgId()));
            map.put(i.getUserId(), orgNameList);
          } else {
            map.get(i.getUserId()).add(orgId2OrgNameMap.get(i.getOrgId()));
          }
        });

    return map;
  }

  @Override
  public void removeUserFromOrg(String userId, String orgId) {
    UserOrg userOrg =
        this.lambdaQuery().eq(UserOrg::getUserId, userId).eq(UserOrg::getOrgId, orgId).one();
    if (CommonUtil.isNullOrEmpty(userOrg)) {
      throw new RRException("该用户不属于该组织，请选择用户对应的机构树然后删除");
    }

    this.removeById(userOrg.getId());
  }
}
